package com.sanyang.pda.presenter;


import com.google.gson.Gson;
import com.sanyang.pda.bean.GoodsDetailBean;
import com.sanyang.pda.bean.UnderDetectionBean;
import com.sanyang.pda.bean.request.UnderDetectionReuqest;
import com.sanyang.pda.contract.UnderDetectionContract;
import com.sanyang.pda.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class UnderDetectionPresenter extends BasePresenter<UnderDetectionContract.View> implements UnderDetectionContract.Presenter {

    @Override
    public void waitDetectList(String en) {
        NetUtils.geApi().waitDetectList(en)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<UnderDetectionBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<UnderDetectionBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void samplingResult(List<UnderDetectionReuqest> underDetectionReuqest) {
        RequestBody requestBody = RequestBody.create(MediaType.parse("\"application/json;charset=UTF-8\""), new Gson().toJson(underDetectionReuqest));
        NetUtils.geApi().samplingResult(requestBody)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<GoodsDetailBean>>() {
                    @Override
                    public void accept(BaseAppEntity<GoodsDetailBean> goodsDetailBeanBaseAppEntity) throws Exception {
                        getView().samplingSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
